<template>
  <div>
    <!-- 调拨查询-->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        columnsRef="zjallocationQuery"
        :isSelectColumn="false"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" :click="addHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <BaseDialog :visible.sync="isEmit" :title="'详情'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <detailModel :rowData="formData" @close="emitcloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :type="type" @close="ownercloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, exportList,exporturl } from '@/api/omSale/allocation/allocationQuery/index.js';
import { dictionary } from '@/utils/dictMixin';
import ownerSeacher from './ownerSeacher';
import { exportExcelFile } from '@/utils';
import detailModel from './detailModel';
export default {
  name: 'WarehouseMaintenance',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        xhrAbort: true
      },
      formData: {},
      exportLoading: false,
      iswarehouse: false,
      isEmit: false,
      issearch: false,
      fromDealer: '',
      toDealer: '',
      type: 0
    };
  },
  mixins: [dictionary],
  components: {
    detailModel,
    ownerSeacher
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '调拨单号',
          fieldName: 'applyNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '调出方',
          fieldName: 'dealerName',
          placeholder: '请选择',
          readonly: false,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change: p => {
              this.fromDealer = p;
          }
        },
        {
          type: 'INPUT',
          label: '调入方',
          fieldName: 'todealerName',
          placeholder: '请选择',
          readonly: false,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          change: p => {
              this.toDealer = p;
          }
        },
        {
          type: 'SELECT',
          label: '调拨状态',
          fieldName: 'applyStatus',
          placeholder: '请选择',
          itemList: [
            { text: '审核中', value: 60121002 },
            { text: '审核通过', value: 60121003 },
            { text: '审核驳回', value: 60121004 },
            { text: '已完成', value: 60121005 },
            { text: '已取消', value: 60121006 }
          ]
        },
        {
          type: 'RANGE_DATE',
          label: '调出申请时间',
          fieldName: 'startApplyDate|endApplyDate',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  详情
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'applyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '审核中', value: 60121002 },
            { text: '审核通过', value: 60121003 },
            { text: '审核驳回', value: 60121004 },
            { text: '已完成', value: 60121005 },
            { text: '已取消', value: 60121006 }
          ],
          render: p => {
            return <span>{this.createDictText(p.row.applyStatus, '6012')}</span>;
          }
        },
        {
          title: '调拨单号',
          dataIndex: 'applyNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调出方经销商代码',
          dataIndex: 'fromDealer',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调出方',
          dataIndex: 'fromDealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调入方经销商代码',
          dataIndex: 'toDealer',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调入方',
          dataIndex: 'toDealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调出方申请时间',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '调拨人',
          dataIndex: 'applyPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '审批通过日期',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '调拨说明',
          dataIndex: 'applyRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    seacherFun(type) {
      this.type = type;
      this.issearch = true;
    },
    selectChangeHandle(row) {},
    // async getStorageList(){
    //   let res=await getStorageList()
    //   console.log(res,'res')
    //   this.topFilterList.map(x=>{
    //     if(x.fieldName==''){
    //       x.itemList=res.data.map(x=>({text:'',value:''}))
    //     }
    //   })
    // },
   async addHandle() {

      let json = Object.assign({}, { CODE: 'CODE_0000050' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }

      // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     let json = this.fetchParams;
      //     json.fromDealer = this.fromDealer;
      //     json.toDealer = this.toDealer;
      //     this.exportLoading = true;

      //     exportList(json)
      //       .then(response => {
      //         console.log(response, 'response');
      //         exportExcelFile(response.data, '调拨查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     console.log('456');
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      debugger
      params.fromDealer = this.fromDealer;
      params.toDealer = this.toDealer;
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    ownercloseHandler(val, type) {
      if (val) {
        console.log(val);
        if (type == 1) {
          this.topFilterList.map(x => {
            if (x.fieldName == 'dealerName') {
              x.initialValue = val.dealerCode;
              this.fromDealer = val.dealerCode;
            }
          });
        } else {
          this.topFilterList.map(x => {
            if (x.fieldName == 'todealerName') {
              x.initialValue = val.dealerCode;
              this.toDealer = val.dealerCode;
            }
          });
        }
      }
      this.issearch = false;
    },
    editHandle(data, type) {
      this.formData = data;
      this.isEmit = true;
    },
    emitcloseHandler(val) {
      this.isEmit = false;
    },
    resetChange(val) {
      this.fromDealer = '';
      this.toDealer = '';
      this.fetchParams.fromDealer = '';
      this.fetchParams.toDealer = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
